package com.wptk.filemanager.ui.file.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wptk.filemanager.R;
import com.wptk.filemanager.model.file.FileBean;
import com.wptk.filemanager.model.file.Video;
import com.wptk.filemanager.model.file.VideoFolderBean;
import com.wptk.filemanager.ui.file.DocsActivity;
import com.kathline.library.util.ZFileOtherUtil;
import com.kathline.library.util.ZFileUtil;

import java.util.ArrayList;
import java.util.List;

public class DocsRVAdapter extends RecyclerView.Adapter<DocsRVAdapter.ViewHolder> {

    private Context context;
    private List<FileBean> datas =  new ArrayList<>();
    private List<FileBean> selectDocs = new ArrayList<>();
    private boolean isSelectModel = false;

    private OnLongClickListener onLongClickListener;
    private OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }

    public void setDatas(List<FileBean> datas) {
        if(datas == null || datas.isEmpty()){
            this.datas.clear();
        }else{
            this.datas.clear();
            this.datas.addAll(datas);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        this.datas.clear();
        notifyDataSetChanged();
    }

    public List<FileBean> getSelectDocs() {
        return selectDocs;
    }

    public void setSelectModel(boolean selectModel) {
        isSelectModel = selectModel;
    }

    public boolean isSelectModel() {
        return isSelectModel;
    }

    public DocsRVAdapter(Context context) {
        this.context = context;
    }

    public void checkAllOf(boolean check){
        if (check){
            selectDocs = new ArrayList<>(datas);
        }else {
            selectDocs.clear();
        }
        DocsRVAdapter.this.notifyItemRangeChanged(0,datas.size());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.docs_rv_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final FileBean fileBean = datas.get(position);
        //final String stuff = fileBean.getFilePath().substring(fileBean.getFilePath().lastIndexOf(File.separator)+1);
        holder.name.setText(fileBean.getFileName());
        holder.iv.setImageResource(fileBean.getIconId());
        holder.date.setText(ZFileOtherUtil.getFormatFileDate(fileBean.getDate()*1000));
        holder.size.setText(ZFileUtil.getFileSize(fileBean.getSize()));

        if(isSelectModel){
            holder.cb.setVisibility(View.VISIBLE);
            if(selectDocs.contains(fileBean)){
                holder.cb.setChecked(true);
            }else{
                holder.cb.setChecked(false);
            }
        }else{
            holder.cb.setVisibility(View.INVISIBLE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isSelectModel){
                    if(selectDocs.contains(fileBean)){
                        selectDocs.remove(fileBean);
                        holder.cb.setChecked(false);
                    }else{
                        selectDocs.add(fileBean);
                        holder.cb.setChecked(true);
                    }
                }

                if(onClickListener != null){
                    onClickListener.onClick(v, fileBean);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                if(onLongClickListener != null && !isSelectModel){
                    onLongClickListener.onLongClick(fileBean);
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv;
        private TextView name;
        private TextView size;
        private TextView date;
        private CheckBox cb;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv);
            name = (TextView) itemView.findViewById(R.id.name);
            size = (TextView) itemView.findViewById(R.id.size);
            date = (TextView) itemView.findViewById(R.id.date);
            cb = (CheckBox) itemView.findViewById(R.id.cb);
        }
    }

    public static interface OnLongClickListener{
        void onLongClick(FileBean fileBean);
    }

    public static interface OnClickListener{
        void onClick(View view, FileBean fileBean);
    }


}
